﻿using System;
using UnityEngine;

namespace WentStateMachine
{
    public enum LoggerLevel
    {
        Info = 0,
        Warrning = 1,
        Error = 2,
        None = 3
    }

    //
    // Logging of messages
    //

    public static class Logger
    {
#if DEBUG_STATEMACHINE
        public static bool Enable = true;
#else
        public static bool Enable = false;
#endif
        public static LoggerLevel Level = LoggerLevel.None;
        static bool forwardToDebug = true;

        public static void Log(string message)
        {
            if (!Enable)
                return;
            if ((int)Level > (int)LoggerLevel.Info)
                return;

            if (forwardToDebug)
                Debug.Log(message);
            else
                _Log(message);
        }

        static void _Log(string message)
        {
            Console.Write(Time.frameCount + ": " + message);
        }

        public static void LogError(string message)
        {
            if (!Enable)
                return;
            if ((int)Level > (int)LoggerLevel.Error)
                return;

            if (forwardToDebug)
                Debug.LogError(message);
            else
                _LogError(message);
        }

        static void _LogError(string message)
        {
            Console.Write(Time.frameCount + ": [ERR] " + message);
        }

        public static void LogWarning(string message)
        {
            if (!Enable)
                return;
            if ((int)Level > (int)LoggerLevel.Warrning)
                return;

            if (forwardToDebug)
                Debug.LogWarning(message);
            else
                _LogWarning(message);
        }

        static void _LogWarning(string message)
        {
            Console.Write(Time.frameCount + ": [WARN] " + message);
        }

        public static void Assert(bool condition)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED");
        }

        public static void Assert(bool condition, string msg)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + msg);
        }

        public static void Assert<T>(bool condition, string format, T arg1)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + string.Format(format, arg1));
        }

        public static void Assert<T1, T2>(bool condition, string format, T1 arg1, T2 arg2)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + string.Format(format, arg1, arg2));
        }

        public static void Assert<T1, T2, T3>(bool condition, string format, T1 arg1, T2 arg2, T3 arg3)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + string.Format(format, arg1, arg2, arg3));
        }

        public static void Assert<T1, T2, T3, T4>(bool condition, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + string.Format(format, arg1, arg2, arg3, arg4));
        }

        public static void Assert<T1, T2, T3, T4, T5>(bool condition, string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
        {
            if (!condition)
                throw new ApplicationException("GAME ASSERT FAILED : " + string.Format(format, arg1, arg2, arg3, arg4, arg5));
        }
    }
}
